home *** CD-ROM | disk | FTP | other *** search
Wrap
var webde = webde ? webde : {}; webde.dienste = webde.dienste ? webde.dienste : {}; webde.dienste.kostenprotokoll = function() { var that = this; // used for anonymous functions (threading) and to give private methods access to object properties /** * Map datastore fields to human readable labels */ var datastoreFields = { ca_User : 0, r_Costs : 1, d_Begin : 2, i_Duration : 3, i_DurationInSec: 4, ca_Provider : 5, i_Clock : 6, r_CostPerMin : 7, ca_TariffName : 8, ca_PhoneNumber : 9, r_CostPerCall : 10, b_ChannelBundeled : 11, i_UserId : 12 }; this.sortBy = 'd_Begin'; // contains the sort column this.sortDirection = { // contains the sort direction for each column d_Begin: 'desc', i_DurationInSec: 'desc', i_Clock: 'desc', r_CostPerMin: 'desc', r_CostPerCall: 'desc', ca_Provider: 'desc', ca_PhoneNumber: 'desc', r_Costs: 'desc' }; this.criticalDatastoreLength = 300; // if datastore has more entries than this number show the modal panel this.sum = 0; // holds the sum for the chosen entries this.duration = 0; // holds the connection duration for the chosen entries this.minDate = ''; this.minDisplayDate = ''; this.maxDate = ''; this.tdMotherObj = document.createElement('TD'); this.trMotherObj = document.createElement('TR'); this.displayedResults = 0; // Number of displayed results this.datastoreLength = 0; // Number of fields in the datastore // HTML Object names: IDs of HTML elements used for displaying information this.HTMLObjectNames = { list: 'Output', modalPanelBG: 'ModalBG', modalPanel: 'ModalPanel', modalPanelStatus: 'ModalPanelTxt', sum: 'Sum', selector: 'Connections', duration: 'Duration', dateFrom: 'DateFrom', dateTo: 'DateTo', fulltext: 'Fulltext', notification: 'NoResultNotification', notificationH: 'NotificationH', notificationTxt: 'NotificationTxt', listTable: 'ListTable', CSVArea: 'CSVArea', CSVBox: 'CSVBox', CSVTextarea: 'CSVTextarea' }; // ------------------------------------------------------------------------- // Private methods: // ------------------------------------------------------------------------- /** * Constructor * * Get HTML-Objects and init values */ function _init() { that.listObj = document.getElementById(that.HTMLObjectNames.list); that.modalPanelBGObj = document.getElementById(that.HTMLObjectNames.modalPanelBG); that.modalPanelObj = document.getElementById(that.HTMLObjectNames.modalPanel); that.modalPanelStatusObj = document.getElementById(that.HTMLObjectNames.modalPanelStatus); that.sumObj = document.getElementById(that.HTMLObjectNames.sum); that.durationObj = document.getElementById(that.HTMLObjectNames.duration); that.selectorObj = document.getElementById(that.HTMLObjectNames.selector); that.dateFromObj = document.getElementById(that.HTMLObjectNames.dateFrom); that.dateToObj = document.getElementById(that.HTMLObjectNames.dateTo); that.fulltextObj = document.getElementById(that.HTMLObjectNames.fulltext); that.noctificationObj = document.getElementById(that.HTMLObjectNames.notification); that.noctificationHObj = document.getElementById(that.HTMLObjectNames.notificationH); that.noctificationTxtObj = document.getElementById(that.HTMLObjectNames.notificationTxt); that.listTableObj = document.getElementById(that.HTMLObjectNames.listTable); that.CSVAreaObj = document.getElementById(that.HTMLObjectNames.CSVArea); that.CSVBox = document.getElementById(that.HTMLObjectNames.CSVBox); that.CSVTextarea = document.getElementById(that.HTMLObjectNames.CSVTextarea); that.pPrepareDatastore(); if (typeof datastore === 'object' && datastore[0]) { that.datastoreLength = datastore[0].length; } if (document.getElementById) { _setInitialStartDate(); that.pPrintDates(); that.pSort('d_Begin'); that.pSelect(); } } /** * Create a Table row object * * @param {array} values for TD-Elements * @param {string} classname: classname of TR-Element * @return {object} DOM TR-Object */ function _createTableRow(values, classname) { var i; var td_obj, txt_obj; var tr_obj = that.trMotherObj.cloneNode(false); var classNames = [ 'l', 'l', 'l', 'r', 'r', 'r', 'l', 'r' ]; // Set classname for tr if (classname !== '') { tr_obj.className = classname; } // Iterate over td cells for (i = 0; i < values.length; i++) { td_obj = that.tdMotherObj.cloneNode(false); td_obj.innerHTML = values[i]; td_obj.className = classNames[i]; tr_obj.appendChild(td_obj); } return tr_obj; } /** * Create a CSV row string * * @param {array} values for TD-Elements * @param {string} classname: classname of TR-Element * @return {string} ;separated list of columns */ function _createCsvRow(values) { var i; var row = ''; // Iterate over cells for (i = 0; i < values.length; i++) { if (i !== 0) { row += "\t" + values[i]; } else { row += values[i]; } } return row; } /** * Fill a value with preceding zeros * * @param {string} value * @param {integer} length * @return {string} */ function _zeroFill(value, fillLength) { var i; value = value.toString(); for (i = value.length; i < fillLength; i++) { value = '0' + value; } return value; } /** * Convert seconds to HH:MM:SS * * @param {integer} * @return {string} */ function _secondsToString(secs) { var hours, minutes; // transform to hours, minutes and seconds hours = Math.floor(secs / 60 / 60); secs -= (hours * 60 * 60); minutes = Math.floor(secs / 60); secs -= (minutes * 60); hours = hours.toString(); minutes = minutes.toString(); secs = secs.toString(); // shift single values with a 0 if (hours.length < 2) { hours = '0' + hours; } if (minutes.length < 2) { minutes = '0' + minutes; } if (secs.length < 2) { secs = '0' + secs; } return hours + ':' + minutes + ':' + secs; } /** * Convert a string in format "HH:MM:SS" to seconds * * @param {string} * @return {integer} */ function _getDurationInSecs(str) { var secs = 0; var time = str.split(':'); secs += parseInt(time[0] * 60 * 60, 10); secs += parseInt(time[1] * 60, 10); secs += parseInt(time[2], 10); return secs; } /** * Convert a string in german date format to a date object. * * @param {string} txt * @return {date} */ function _germanDateToDate(txt) { var enDate = txt.substr(6, 4) + '/' + txt.substr(3, 2) + '/' + txt.substr(0,2); return new Date(enDate); } /** * Return german date from a date object. * * @param {date} * @return {string} */ function _dateToGermanDate(dateObj) { var newDate = _zeroFill(dateObj.getDate(), 2) + '.' + _zeroFill(dateObj.getMonth() + 1, 2) + '.' + dateObj.getFullYear(); return newDate; } /** * Get the day before today. * * @see _zeroFill * @param {string} str_day * @return {date} */ function _getYesterday(day) { var calday = new Date(day.getTime() - (24 * 60 * 60 * 1000)); return calday; } /** * Get the next day * * @see _zeroFill * @param {string} str_day * @return {date} */ function _getTomorrow(day) { var calday = new Date(day.getTime() + (24 * 60 * 60 * 1000)); return calday; } /** * Set the initial start date for the list display. * * Search the first of the preceding month starting with today. */ function _setInitialStartDate() { var calMonth = new Date().getMonth(); var calYear = new Date().getFullYear(); if (calMonth === 0) { calMonth = 12; calYear -= 1; } that.minDisplayDate = new Date(calYear + '/' + calMonth + '/1'); } /** * Display the notification area and hide the result table */ function _showNotification(headline, text) { if (headline !== '') { that.noctificationHObj.innerHTML = headline; } if (text !== '') { that.noctificationTxtObj.innerHTML = text; } that.noctificationObj.style.display = 'block'; that.listTableObj.style.display = 'none'; that.CSVAreaObj.style.display = 'none'; } /** * Display the result table and hide the notification area */ function _showResultTable() { that.noctificationObj.style.display = 'none'; that.listTableObj.style.display = 'block'; that.CSVAreaObj.style.display = 'block'; } // ------------------------------------------------------------------------- // Privileged methods: // ------------------------------------------------------------------------- // --------------- Modal panel /** * Show the modal panel * * @param {string} */ this.pShowModalPanel = function(statusText) { if (typeof statusText === 'string') { this.modalPanelStatusObj.innerHTML = statusText; } this.modalPanelObj.style.visibility = 'visible'; this.modalPanelBGObj.style.visibility = 'visible'; }; /** * Hide the modal panel */ this.pHideModalPanel = function() { this.modalPanelObj.style.visibility = 'hidden'; this.modalPanelBGObj.style.visibility = 'hidden'; }; // --------------- Information display /** * Print total of connection costs */ this.pPrintSum = function() { this.sumObj.innerHTML = this.sum.toFixed(2); }; /** * Print total of connection durations */ this.pPrintDuration = function() { this.durationObj.innerHTML = _secondsToString(this.duration); }; /** * Print min and max date to HTML form */ this.pPrintDates = function() { var today = new Date(); this.dateFromObj.value = _dateToGermanDate(this.minDisplayDate); this.dateToObj.value = _dateToGermanDate(today); }; // --------------- List table manipulation /** * Update sort indicators in the table header * * Sort indicators are set via the id-attribute in a span element in a th-element. */ this.pUpdateTableHeader = function() { var i; var indicators = document.getElementById('THead').getElementsByTagName('SPAN'); var mapper = {d_Begin: 0, i_DurationInSec: 6, i_Clock: 5, r_CostPerMin: 3, r_CostPerCall: 4, ca_Provider: 1, ca_PhoneNumber: 2, r_Costs: 7}; // Clear all IDs for (i = 0; i < indicators.length; i++) { indicators[i].id = ''; } // Set new ID if (this.sortDirection[this.sortBy] === 'asc') { indicators[mapper[this.sortBy]].id = 'focus-down'; } else { indicators[mapper[this.sortBy]].id = 'focus-up'; } }; /** * Delete the complete list table */ this.pDeleteTable = function() { var i; var rows = this.listObj.childNodes; for (i = this.listObj.childNodes.length-1; i > -1; i--) { this.listObj.deleteRow(i); } }; /** * Get the min and max date and set the display flag. */ this.pPrepareDatastore = function() { var tr_obj, ds_date, className; var classname = ''; var ds_length = datastore.length; var i = 0; var count = 0; eval("var rowRef = window.dataindex_" + fieldindexorder[this.sortBy]); for (i = 0; i < ds_length; i++) { // Prepare display flag if (typeof datastore[i][this.datastoreLength] !== 'number') { datastore[i][this.datastoreLength] = 1; } // Get min and max date ds_date = _germanDateToDate(datastore[i][fieldindexorder.d_Begin]); if (this.minDate === '' || this.minDate > ds_date) { this.minDate = ds_date; } if (this.maxDate === '' || this.maxDate < ds_date) { this.maxDate = ds_date; } } }; /** * Update the list table * * @param {string} */ this.pUpdateTable = function(row) { var tRow; var count = 0; var dl = datastore.length; this.sum = 0; this.duration = 0; var i = 0; var subConStore = []; // Temp store for sub connections eval("var rowRef = window.dataindex_" + fieldindexorder[row]); for (i = 0; i < dl; i++) { if (datastore[rowRef[i]][this.datastoreLength] === 1) { className = count % 2 ? 'alt' : ''; // create and fill tr-object tr_obj = _createTableRow([ datastore[rowRef[i]][datastoreFields.d_Begin] + " " + datastore[rowRef[i]][datastoreFields.i_Duration], datastore[rowRef[i]][datastoreFields.ca_Provider], datastore[rowRef[i]][datastoreFields.ca_PhoneNumber], datastore[rowRef[i]][datastoreFields.r_CostPerMin], datastore[rowRef[i]][datastoreFields.r_CostPerCall], datastore[rowRef[i]][datastoreFields.i_Clock], _secondsToString(datastore[rowRef[i]][datastoreFields.i_DurationInSec]) + " ", datastore[rowRef[i]][datastoreFields.r_Costs] + " " ], className); // differentiate between main- and subconnections if (datastore[rowRef[i]][datastoreFields.b_ChannelBundeled] === 0) { // Mainconnections this.listObj.appendChild(tr_obj); // if there are subconnections then print'em if (subConStore.length > 0) { for (var k = 0; k < subConStore.length; k++) { this.listObj.appendChild(subConStore[k]); } subConStore = []; } } else { // Subconnections // Alter visual presentation by adjusting the classname (in tr-object) // : wait for webprod template this.changeConnectionToSubConnection(tr_obj); // save subconnections temporarily if sortdirection is descending, // otherwise append them directly if (this.sortDirection[this.sortBy] === 'asc') { subConStore.push(tr_obj); } else { this.listObj.appendChild(tr_obj); } } // Build sum of costs this.sum += parseFloat(datastore[rowRef[i]][1].replace(',', '.'), 2); // Calculate overall duration this.duration += datastore[rowRef[i]][4]; count++; } } }; /** * Print the result to the output object * * This method threads itself according to the action variable, so the modal panel * is displayed before the calculation starts. * * @param {string} action: showPanel|printTable|hidePanel */ this.pDoPrint = function(action) { if (!action) { action = 'showPanel'; } if (datastore.length === 0) { _showNotification('Es liegen bisher keine Verbindungsdaten vor.', ''); } else if (this.displayedResults === 0) { _showNotification('Für Ihre Auswahl konnten keine Verbindungsdaten gefunden werden.', 'Bitte erweitern Sie den Zeitraum über die Filteroptionen oben.'); } else { _showResultTable(); if (action === 'showPanel') { if (this.displayedResults > this.criticalDatastoreLength) { this.pShowModalPanel(); } window.setTimeout( function() { that.pDoPrint('printTable'); }, 10); } if (action === 'printTable') { this.pUpdateTable(this.sortBy); this.pUpdateTableHeader(); this.pPrintSum(); this.pPrintDuration(); this.pPrintDates(); window.setTimeout( function() { that.pDoPrint('hidePanel'); }, 10); } if (action === 'hidePanel') { this.pHideModalPanel(); } } }; // --------------- Sort methods /** * Sort the datastore index asc or desc * * If the same column is clicked again reverse the sort direction. * If another column is chosen take the sort direction from the * previous column. * * @param {integer} */ this.pSort = function(row) { eval("var rowRef = window.dataindex_" + fieldindexorder[row]); if (row === this.sortBy) { if (this.sortDirection[row] === 'desc') { this.sortDirection[row] = 'asc'; rowRef.reverse(); } else { this.sortDirection[row] = 'desc'; rowRef.reverse(); } } else { if (this.sortDirection[row] !== this.sortDirection[this.sortBy]) { rowRef.reverse(); } this.sortDirection[row] = this.sortDirection[this.sortBy]; } }; /** * Wrapper for the sort function * * This method threads itself according to the action variable, so the modal panel * is displayed before the calculation starts. * * @param {object} domObj * @param {string} sortType * @param {string} action: showPanel|printTable|hidePanel */ this.pDoSort = function(action, row) { if (!action) { action = 'showPanel'; } if (action === 'showPanel') { if (this.displayedResults > this.criticalDatastoreLength) { this.pShowModalPanel('Sortiere Tabelle'); } window.setTimeout( function() { that.pDoSort('sort', row); }, 10); } if (action === 'sort') { this.pSort(row); this.pDeleteTable(); this.pUpdateTable(row); this.sortBy = row; this.pUpdateTableHeader(); window.setTimeout( function() { that.pDoSort('hidePanel', row); }, 10); } if (action === 'hidePanel') { this.pHideModalPanel(); } return false; }; // --------------- Select methods /** * Narrow the datastore selection to the users' input */ this.pSelect = function() { var i, k; var conCost; var dl = datastore.length; var takeover = true; var chosenCon = this.selectorObj.value; var fulltext = this.fulltextObj.value; var date_from = _germanDateToDate(this.dateFromObj.value); var date_to = _germanDateToDate(this.dateToObj.value); var date_cur; var ftSearchMinus = []; var ftSearchPlus = []; var ftSearchPlusTmp = []; var searchfield = this.fulltextObj.value; // Define negative fulltext search criteria var negSearch = searchfield.match(/(\s\-{1,}|^\-)(.[^\s]*)/g); if (negSearch) { for (i = 0; i < negSearch.length; i++) { if (negSearch[i].substr(0,1) === '-') { ftSearchMinus.push(negSearch[i].substr(1).toLowerCase()); } else { ftSearchMinus.push(negSearch[i].substr(2).toLowerCase()); } searchfield = searchfield.replace(negSearch[i], ''); } } // Define positive fulltext search criteria searchfield = searchfield.replace('+', ''); ftSearchPlusTmp = searchfield.split(' '); for (i = 0; i < ftSearchPlusTmp.length; i++) { if (ftSearchPlusTmp[i] !== '') { if (ftSearchPlusTmp[i].substr(0,1) === '+') { ftSearchPlus.push(ftSearchPlusTmp[i].substr(1)); } else { ftSearchPlus.push(ftSearchPlusTmp[i]); } } } this.displayedResults = 0; for (i = 0; i < dl; i++) { // Connection if (chosenCon === '*') { takeover = true; } else { conCostPerMin = parseFloat(datastore[i][7].replace(',', '.')); conCostPerCall = parseFloat(datastore[i][10].replace(',', '.')); if (chosenCon === '1') { if (conCostPerCall > 0) { takeover = true; } else { takeover = false; } } else if (chosenCon === '2') { if (conCostPerCall === 0) { takeover = true; } else { takeover = false; } } else if (chosenCon === '3') { if (conCostPerMin > 1.5) { takeover = true; } else { takeover = false; } } else { takeover = false; } } // Date from to if (takeover) { date_cur = _germanDateToDate(datastore[i][2]); if (date_cur >= date_from && date_cur <= date_to) { takeover = true; } else { takeover = false; } } // Fulltext if (takeover && this.fulltextObj.value !== '') { for (m = 0; m < ftSearchMinus.length; m++) { if (datastore[i][5].toLowerCase().indexOf(ftSearchMinus[m]) > -1) { takeover = false; } else if (datastore[i][9].toLowerCase().indexOf(ftSearchMinus[m]) > -1) { takeover = false; } } if (takeover) { tmp_takeover = true; for (m = 0; m < ftSearchPlus.length; m++) { if ((datastore[i][5].toLowerCase().indexOf(ftSearchPlus[m].toLowerCase()) > -1 || datastore[i][9].indexOf(ftSearchPlus[m].toLowerCase()) > -1) && tmp_takeover) { tmp_takeover = true; } else { tmp_takeover = false; } } if (tmp_takeover) { takeover = true; } else { takeover = false; } } } if (takeover) { datastore[i][this.datastoreLength] = 1; this.displayedResults++; } else { datastore[i][this.datastoreLength] = 0; } } }; /** * Wrapper for the select function * * This method threads itself according to the action variable, so the modal panel * is displayed before the calculation starts. * * @param {string} action */ this.pDoSelect = function(action) { if (datastore.length > 0) { if (!action) { action = 'showPanel'; } if (action === 'showPanel') { if (this.displayedResults > this.criticalDatastoreLength) { this.pShowModalPanel('Durchsuche Tabelle'); } window.setTimeout( function() { that.pDoSelect('select'); }, 10); } if (action === 'select') { this.pSelect(); if (this.displayedResults === 0) { _showNotification('Für Ihre Auswahl konnten keine Verbindungsdaten gefunden werden.', 'Bitte ändern Sie den Zeitraum, in dem gesucht werden soll oder heben Sie die Sucheinschränkungen auf.'); } else { _showResultTable(); this.pDeleteTable(); this.pUpdateTable(this.sortBy); this.pPrintSum(); this.pPrintDuration(); } window.setTimeout( function() { that.pDoSelect('hidePanel'); }, 10); } if (action === 'hidePanel') { this.pHideModalPanel(); } } return false; }; // --------------- Calendar popups /** * Calendar functionality: Show dateFrom-Calendar and adjust non-selectable days */ this.pSelectFromDate = function() { if (this.minDate) { var endDate = this.dateToObj.value; endDate = _germanDateToDate(endDate); // hide select box if (document.all) { this.selectorObj.style.visibility = 'hidden'; } calFrom.clearDisabledDays(); calFrom.addDisabledDates(null, formatDate(_getYesterday(this.minDate), 'yyyy-MM-dd')); calFrom.addDisabledDates(formatDate(_getTomorrow(endDate), 'yyyy-MM-dd'), null); calFrom.select(this.dateFromObj,'DateFrom','dd.MM.yyyy'); calFrom.refresh(); } }; /** * Calendar functionality: Show dateTo-Calendar and adjust non-selectable days */ this.pSelectToDate = function() { if (this.minDate) { var startDate = this.dateFromObj.value; var today = new Date(); startDate = _germanDateToDate(startDate); // hide select box if (document.all) { this.selectorObj.style.visibility = 'hidden'; } calTo.clearDisabledDays(); calTo.addDisabledDates(null, formatDate(_getYesterday(startDate), 'yyyy-MM-dd')); calTo.addDisabledDates(formatDate(_getTomorrow(today), 'yyyy-MM-dd'), null); calTo.select(this.dateToObj,'DateTo','dd.MM.yyyy'); calTo.refresh(); } }; // --------------- CSV export /** * Do the CSV export */ this.pDoCsvExport = function() { var tRow; var count = 0; var dl = datastore.length; var i = 0; this.sum = 0; this.duration = 0; var output = ''; this.selectorObj.style.visibility = 'hidden'; this.CSVBox.style.display = 'block'; eval("var rowRef = window.dataindex_" + fieldindexorder[this.sortBy]); for (i = 0; i < dl; i++) { if (datastore[rowRef[i]][this.datastoreLength] === 1) { output += (_createCsvRow([datastore[rowRef[i]][2] + " " + datastore[rowRef[i]][3], datastore[rowRef[i]][5], ""+datastore[rowRef[i]][9], datastore[rowRef[i]][7], datastore[rowRef[i]][10], datastore[rowRef[i]][6], datastore[rowRef[i]][4], datastore[rowRef[i]][1] ]) + "\r\n"); // Build sum of costs this.sum += parseFloat(datastore[rowRef[i]][1].replace(',', '.'), 2); // Calculate overall duration this.duration += _getDurationInSecs(datastore[rowRef[i]][fieldindexorder.i_DurationInSec]); count++; } } this.CSVTextarea.value = output; return false; }; this.pPrintSysData = function() { var datafields = ['smversion', 'datefrom', 'username', 'os', 'cpu', 'ram', 'modem', 'dialstring']; var i; //prepare data fields sysdata.datefrom = _dateToGermanDate(new Date(sysdata.datefrom.substr(0, 10).replace(/-/g, '/'))) + ', ' + sysdata.datefrom.substr(11, 5) + ' Uhr'; for (i in datafields) { if (!sysdata[datafields[i]] || sysdata[datafields[i]] === '') { sysdata[datafields[i]] = '---'; } document.getElementById('Sys' + i).innerHTML = sysdata[datafields[i]]; } }; // Call constructor _init(); }; // ----------------------------------------------------------------------------- // Public methods: // ----------------------------------------------------------------------------- /** * Print the result list */ webde.dienste.kostenprotokoll.prototype.printList = function() { // Display notification if there are more than 3000 datafields var obj = document.getElementById('DataNotification'); if (sysdata.dataCount > 3000) { obj.style.display = 'block'; } else { obj.style.display = 'none'; } this.pDoPrint('showPanel'); }; /** * Get Sysdata and print it to the sysdata-list */ webde.dienste.kostenprotokoll.prototype.printSysdata = function() { this.pPrintSysData(); }; webde.dienste.kostenprotokoll.prototype.closeCSVWindow = function() { this.CSVBox.style.display = 'none'; this.selectorObj.style.visibility = 'visible'; return false; }; /** * Changes a tr-html-object so that it represents a subconnection. * * @param {Object} trObj */ webde.dienste.kostenprotokoll.prototype.changeConnectionToSubConnection = function(trObj) { var tdCell; if (tdCell = trObj.getElementsByTagName('TD')[0]) { var tdCellValue = tdCell.innerHTML; var tdCellNewValue = '<table border="0" cellspacing="0" cellpadding="0" width="140" style="margin-bottom:0px;"><tr><td class="kanalbuendelung" style="width:12px;"></td><td class="kanalbuendelung" style="width:1px;"><img src="img/linie.gif" width="5" height="24" alt="" border="0" /></td><td class="kanalbuendelung" style="width:4px;"></td><td class="kanalbuendelung" style="width:118px;">'+tdCellValue+'</td></tr></table>'; tdCell.innerHTML = tdCellNewValue; } }